/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.table.alarm;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.Alarm;
import twaver.AlarmProbableCause;
import twaver.AlarmSeverity;
import twaver.AlarmTrendIndication;
import twaver.AlarmVisibleFilter;
import twaver.EnumType;
import twaver.EnumTypeManager;
import twaver.TDataBox;
import twaver.TWaverUtil;
import twaver.TaskAdapter;
import twaver.TaskScheduler;
import twaver.base.enumerable.EnumerableManager;
import twaver.Node;
import twaver.table.TAlarmTable;
import twaver.table.TTableColumn;
import twaver.table.TTableNavigator;
import twaver.table.editor.EnumTypeEditor;
import twaver.table.renderer.EnumTypeRenderer;
import demo.DemoPane;

public class AlarmTableDemo extends DemoPane {

    public static boolean flashFlag = false;

    // flash rule 
    public static boolean isInterest(AlarmSeverity severity){
    	if(AlarmSeverity.getSeverityComparator().compare(severity, AlarmSeverity.CRITICAL) >= 0){
    		return true;
    	}
    	return false;
    }
    
    private Object[] values = EnumerableManager.getEnumerables(AlarmProbableCause.class).toArray();
    private TDataBox box = new TDataBox();
    
    final int rowHeight = 28;
    final int desHeight = 40;
    private TAlarmTable alarmTable = new TAlarmTable(box);
    private JCheckBox critical = createCheckBox(AlarmSeverity.CRITICAL);
    private JCheckBox major = createCheckBox(AlarmSeverity.MAJOR);
    private JCheckBox minor = createCheckBox(AlarmSeverity.MINOR);
    private JCheckBox warning = createCheckBox(AlarmSeverity.WARNING);
    private JCheckBox indeterminate = createCheckBox(AlarmSeverity.INDETERMINATE);
    
    private JCheckBox createCheckBox(AlarmSeverity severtiy){
    	JCheckBox c = new JCheckBox(severtiy.getDisplayName(), true);
    	c.setForeground(severtiy.getColor());
    	return c;
    }
    
    public AlarmTableDemo() {
    	EnumTypeManager.getInstance().registerEnumTypes("owner", new EnumType[]{
    			new EnumType(0, "Peter", "/demo/table/alarm/peter.gif"), 
    			new EnumType(1, "Mary", "/demo/table/alarm/mary.gif")});	
    	
        alarmTable.setRowHeight(19);
        alarmTable.getColumnByName("alarmSeverity").setRenderer(new FlashCellRenderer());

        TTableNavigator navigator = new TTableNavigator(alarmTable.getTableModel(), new int[] {20, 30, 50, 0}, false);
        JPanel tablePanel = new JPanel(new BorderLayout());
        tablePanel.add(navigator, BorderLayout.SOUTH);
        tablePanel.add(new JScrollPane(alarmTable), BorderLayout.CENTER);

        JSplitPane split = new JSplitPane();
        split.setOrientation(JSplitPane.HORIZONTAL_SPLIT);
        split.setLeftComponent(tablePanel);
        split.setRightComponent(new TableControlPanel(alarmTable));
        split.setDividerLocation(400);
        this.add(split, BorderLayout.CENTER);

        Node node = new Node("node1");
        node.setName("Router-1");
        box.addElement(node);

        node = new Node("node2");
        node.setName("Router-2");
        box.addElement(node);

        for (int i = 10; i < 60; i++) {
            Alarm alarm = new Alarm("ALM_" + i);
            alarm.putClientProperty("owner", TWaverUtil.valueOf(i%2));
            alarm.setElementID("node" + (i % 2 + 1));
            alarm.setAcked(i % 2 == 1);
            alarm.setAlarmSeverity(TWaverUtil.getRandomNonClearedSeverity());
            alarm.setTrendIndication(AlarmTrendIndication.MORE_SEVERE);
            alarm.setProbableCause(getRandomAlarmProbableCause());
            box.getAlarmModel().addAlarm(alarm);
        }

        TTableColumn ownerColumn = new TTableColumn("owner", "Owner");
        ownerColumn.setPreferredWidth(80);
        ownerColumn.setEditable(true);
        alarmTable.getTableModel().addColumn(ownerColumn);
        ownerColumn.setRenderer(new EnumTypeRenderer("owner"));
        ownerColumn.setEditor(new EnumTypeEditor("owner", "false"));
        
        JPanel filterPane = new JPanel(new FlowLayout());
        tablePanel.add(filterPane, BorderLayout.SOUTH);
        filterPane.add(this.critical);
        filterPane.add(this.major);
        filterPane.add(this.minor);
        filterPane.add(this.warning);
        filterPane.add(this.indeterminate);

        ChangeListener l = new ChangeListener(){
			public void stateChanged(ChangeEvent e) {
				alarmTable.getTableModel().publishData();
			}
        };
        this.critical.addChangeListener(l);
        this.major.addChangeListener(l);
        this.minor.addChangeListener(l);
        this.warning.addChangeListener(l);
        this.indeterminate.addChangeListener(l);
        
        alarmTable.addVisibleFilter(new AlarmVisibleFilter(){
			public boolean isVisible(Alarm alarm) {
				AlarmSeverity severity = alarm.getAlarmSeverity();
				if(severity != null){
			        if(severity.getDisplayName().equals(critical.getText())){
			        	return critical.isSelected();
			        }
			        if(severity.getDisplayName().equals(major.getText())){
			        	return major.isSelected();
			        }
			        if(severity.getDisplayName().equals(minor.getText())){
			        	return minor.isSelected();
			        }
			        if(severity.getDisplayName().equals(warning.getText())){
			        	return warning.isSelected();
			        }
			        if(severity.getDisplayName().equals(indeterminate.getText())){
			        	return indeterminate.isSelected();
			        }
				}
		        return true;
			}
        });
        
		TaskScheduler.getInstance().register(new TaskAdapter(){
			public int getInterval() {
				return 1000;
			}
			public void run(long clock) {
				if(!alarmTable.isShowing()){
					return;
				}
				flashFlag = !flashFlag;
				if(alarmTable.isVisible()){
					TTableColumn column = alarmTable.getColumnByName("alarmSeverity");
					int columnIndex = alarmTable.getTableModel().getPublishedColumn().indexOf(column);
					if(columnIndex >= 0){
						columnIndex = alarmTable.convertColumnIndexToView(columnIndex);
					}
					int count = alarmTable.getRowCount();
					for(int row=0; row<count; row++){
						Alarm alarm = alarmTable.getAlarmByRowIndex(row);
						if(alarm != null){
							if(isInterest(alarm.getAlarmSeverity())){
								alarmTable.repaint(alarmTable.getCellRect(row, columnIndex, false));
							}
						}
					}
				}
			}
		});
    }

    private AlarmProbableCause getRandomAlarmProbableCause() {
        return (AlarmProbableCause) values[TWaverUtil.getRandomInt(values.length)];
    }
}